var ClassFormModel = function (data, validTimeObj) {
  BaseModel.call(this);
  var self = this;

  var fn = {
    initData: function (data, validTimeObj) {
      if (data === undefined) {
        data = {};
      }

      var fields = ['className', 'isNeedRest'];
      self.setDataFromObj(data, fields, '');

      var timeList = data.timeList;
      if (timeList === undefined) {
        self.timeType = 1;
        self.isNeedRest = 0;
        self.resetTimeList();
        return;
      }

      self.id = data.id;
      self.restStartTime = ClassFormModel.getTimeDate(data.restOne);
      self.restEndTime = ClassFormModel.getTimeDate(data.restTwo);
      self.restTimeRange = {};
      self.timeType = timeList.length;

      timeList = _.cloneDeep(timeList);
      timeList = _.map(timeList, function (timeObj) {
        var startTime = timeObj.startTime;
        startTime = moment(startTime).format('HH:mm');
        var validTime = validTimeObj[startTime];

        if (validTime === undefined) {
          timeObj.earlyTime = null;
          timeObj.lateTime = null;
        } else {
          timeObj.earlyTime = ClassFormModel.getTimeDate(validTime.earlyTime);
          timeObj.lateTime = ClassFormModel.getTimeDate(validTime.lateTime);
        }
        return timeObj;
      });

      self.timeList = timeList;
      for (var i = 0; i < timeList.length; i++) {
        var timeObj = timeList[i];
        timeObj.startTimeRange = this.getStartTimeRange(timeObj, i);
        timeObj.endTimeRange = this.getEndTimeRange(timeObj, i);
        timeObj.earlyTimeRange = this.getEarlyTimeRange(timeObj, i);
        timeObj.lateTimeRange = this.getLateTimeRange(timeObj, i);
      }

      this.setRestTimeRange('startRest');
      this.setRestTimeRange('endRest');
      self.calWorkTime();
    },

    getTotalWorkTime: function () {
      var timeList = self.timeList;
      var workTime = 0;
      for (var i = 0; i < timeList.length; i++) {
        var timeObj = timeList[i];
        var time = moment(timeObj.endTime).diff(timeObj.startTime, 'minute');
        workTime += time;
      }

      if (self.timeType === 1 && self.isNeedRest === 1) {
        var restTime = moment(self.restEndTime).diff(self.restStartTime, 'minute');
        workTime -= restTime;
      }
      return workTime;
    },

    getStartTimeRange: function (timeObj, index) {
      var minTime;
      var earlyTime = timeObj.earlyTime;
      if (earlyTime) {
        minTime = moment(earlyTime).format('HH:mm:ss');
      } else {
        var preTimeObj = self.timeList[index - 1];
        if (preTimeObj) {
          var lateTime = preTimeObj.lateTime;
          if (lateTime) {
            minTime = moment(lateTime).format('HH:mm:ss');
          } else {
            minTime = moment(preTimeObj.endTime).format('HH:mm:ss');
          }
        } else {
          minTime = '00:00:00';
        }
      }

      var maxTime = moment(timeObj.endTime).format('HH:mm:ss');
      return `${minTime} - ${maxTime}`;
    },

    getEarlyTimeRange: function (timeObj, index) {
      var minTime;
      var preTimeObj = self.timeList[index - 1];
      if (preTimeObj) {
        var lateTime = preTimeObj.lateTime;
        if (lateTime) {
          minTime = moment(lateTime).format('HH:mm:ss');
        } else {
          minTime = moment(preTimeObj.endTime).format('HH:mm:ss');
        }
      } else {
        minTime = '00:00:00';
      }

      var maxTime = moment(timeObj.startTime).format('HH:mm:ss');
      return `${minTime} - ${maxTime}`;
    },

    getEndTimeRange: function (timeObj, index) {
      var maxTime;
      var lateTime = timeObj.lateTime;
      if (lateTime) {
        maxTime = moment(lateTime).format('HH:mm:ss');
      } else {
        var nextTimeObj = self.timeList[index + 1];
        if (nextTimeObj) {
          var earlyTime = nextTimeObj.earlyTime;
          if (earlyTime) {
            maxTime = moment(earlyTime).format('HH:mm:ss');
          } else {
            maxTime = moment(nextTimeObj.startTime).format('HH:mm:ss');
          }
        } else {
          maxTime = "23:59:00";
        }
      }

      var minTime = moment(timeObj.startTime).format('HH:mm:ss');
      return `${minTime} - ${maxTime}`;
    },

    getLateTimeRange: function (timeObj, index) {
      var maxTime;
      var nextTimeObj = self.timeList[index + 1];
      if (nextTimeObj) {
        var earlyTime = nextTimeObj.earlyTime;
        if (earlyTime) {
          maxTime = moment(earlyTime).format('HH:mm:ss');
        } else {
          maxTime = moment(nextTimeObj.startTime).format('HH:mm:ss');
        }
      } else {
        maxTime = "23:59:00";
      }

      var minTime = moment(timeObj.endTime).format('HH:mm:ss');
      return `${minTime} - ${maxTime}`;
    },

    setRestTimeRange: function (type) {
      var timeObj = self.timeList[0];
      if (type === 'startRest') {
        var minTime = moment(timeObj.startTime).format('HH:mm:ss');
        var maxTime = moment(self.restEndTime).format('HH:mm:ss');
        self.restTimeRange.start = `${minTime} - ${maxTime}`;
      } else if (type === 'endRest') {
        var minTime = moment(self.restStartTime).format('HH:mm:ss');
        var maxTime = moment(timeObj.endTime).format('HH:mm:ss');
        self.restTimeRange.end = `${minTime} - ${maxTime}`;
      }
    }
  };

  this.resetTimeList = function () {
    var timeType = this.timeType;
    var timeWayList = ClassFormModel.timeWayList;
    var timeWay = _.find(timeWayList, {
      type: timeType
    });
    var timeList = timeWay.timeList;
    timeList = _.cloneDeep(timeList);
    this.timeList = timeList;

    if (timeType === 1) {
      this.restStartTime = ClassFormModel.getTimeDate('12:00');
      this.restEndTime = ClassFormModel.getTimeDate('13:00');
      this.restTimeRange = {
        start: '09:00:00 - 13:00:00',
        end: '12:00:00 - 18:00:00'
      };
    }
    this.workTime = fn.getTotalWorkTime();
  };

  this.resetTimeRange = function (type, index) {
    var timeList = this.timeList;
    var timeObj = null;
    if (index !== undefined) {
      timeObj = timeList[index];
    }
    if (type === 'startTime') {
      timeObj.endTimeRange = fn.getEndTimeRange(timeObj, index);
      timeObj.earlyTimeRange = fn.getEarlyTimeRange(timeObj, index);
      var preTimeObj = timeList[index - 1];
      if (preTimeObj) {
        preTimeObj.endTimeRange = fn.getEndTimeRange(preTimeObj, index - 1);
        preTimeObj.lateTimeRange = fn.getLateTimeRange(preTimeObj, index - 1);
      }

      if (this.timeType === 1) {
        fn.setRestTimeRange('startRest');
      }
    } else if (type === 'endTime') {
      timeObj.startTimeRange = fn.getStartTimeRange(timeObj, index);
      timeObj.lateTimeRange = fn.getLateTimeRange(timeObj, index);
      var nextTimeObj = timeList[index + 1];
      if (nextTimeObj) {
        nextTimeObj.startTimeRange = fn.getStartTimeRange(nextTimeObj, index + 1);
        nextTimeObj.earlyTimeRange = fn.getEarlyTimeRange(nextTimeObj, index + 1);
      }

      if (this.timeType === 1) {
        fn.setRestTimeRange('endRest');
      }
    } else if (type === 'earlyTime') {
      timeObj.startTimeRange = fn.getStartTimeRange(timeObj, index);
      var preTimeObj = timeList[index - 1];
      if (preTimeObj) {
        preTimeObj.endTimeRange = fn.getEndTimeRange(preTimeObj, index - 1);
        preTimeObj.lateTimeRange = fn.getLateTimeRange(preTimeObj, index - 1);
      }
    } else if (type === 'lateTime') {
      timeObj.endTimeRange = fn.getEndTimeRange(timeObj, index);
      var nextTimeObj = timeList[index + 1];
      if (nextTimeObj) {
        nextTimeObj.startTimeRange = fn.getStartTimeRange(nextTimeObj, index + 1);
        nextTimeObj.earlyTimeRange = fn.getEarlyTimeRange(nextTimeObj, index + 1);
      }
    } else if (type === 'restStartTime') {
      fn.setRestTimeRange('endRest');
    } else if (type === 'restEndTime') {
      fn.setRestTimeRange('startRest');
    };
  };

  this.calWorkTime = function () {
    this.workTime = fn.getTotalWorkTime();
  };

  this.checkValid = function () {
    var className = this.className;
    if (className === '') {
      return 'className';
    }

    var reg = /^[0-9a-zA-Z\u4e00-\u9fa5]{1,10}$/;
    if (!reg.test(className)) {
      return 'classNameNoMatch';
    }

    var timeList = this.timeList;
    for (var i = 0; i < timeList.length; i++) {
      var timeObj = timeList[i];
      var earlyTime = timeObj.earlyTime;
      var lateTime = timeObj.lateTime;
      if (earlyTime === null && lateTime !== null) {
        return 'earlyTime';
      }

      if (earlyTime !== null && lateTime === null) {
        return 'lateTime';
      }
    }

    return '';
  };

  this.getObject = function () {
    var postData = {};

    var fields = ['className', 'workTime'];
    this.setPostData(postData, fields);

    var id = this.id;
    if (id !== undefined) {
      postData.id = id;
    }

    if (this.timeType === 1) {
      postData.isNeedRest = this.isNeedRest;
      postData.restOne = moment(this.restStartTime).format('HH:mm');
      postData.restTwo = moment(this.restEndTime).format('HH:mm');
    } else {
      postData.isNeedRest = 0;
      postData.restOne = '12:00';
      postData.restTwo = '13:00';
    }

    var timeList = this.timeList;
    var attendanceTimeList = [];
    for (var i = 0; i < timeList.length; i++) {
      var timeObj = timeList[i];
      var startTime = timeObj.startTime;
      startTime = moment(startTime).format('HH:mm');
      var endTime = timeObj.endTime;
      endTime = moment(endTime).format('HH:mm');
      attendanceTimeList.push(`${startTime}-${endTime}`);
    }
    postData.attendanceTime = attendanceTimeList.join(' ');
    return postData;
  };

  this.getValidObject = function (id) {
    var postData = {
      classId: id
    };

    var timeList = this.timeList;
    var validTimeList = [];
    for (var i = 0; i < timeList.length; i++) {
      var timeObj = timeList[i];
      var earlyTime = timeObj.earlyTime;
      var lateTime = timeObj.lateTime;
      if (earlyTime === null && lateTime === null) {
        continue;
      }

      var startValidTime = {
        baseCheckTime: moment(timeObj.startTime).format('HH:mm')
      };
      var endValidTime = {
        baseCheckTime: moment(timeObj.endTime).format('HH:mm')
      }

      if (earlyTime !== null) {
        earlyTime = moment(earlyTime).format('HH:mm');
        startValidTime.baseCheckStartTime = earlyTime;
        endValidTime.baseCheckStartTime = earlyTime;
      }

      if (lateTime !== null) {
        lateTime = moment(lateTime).format('HH:mm');
        startValidTime.baseCheckEndTime = lateTime;
        endValidTime.baseCheckEndTime = lateTime;
      }
      validTimeList.push(startValidTime, endValidTime);
    }
    postData.beans = JSON.stringify(validTimeList);
    return postData;
  };

  fn.initData(data, validTimeObj);
};

ClassFormModel.getTimeDate = function (timeStr) {
  if (SysUtil.isEmpty(timeStr)) {
    return null;
  }
  var time = moment(`${this.date} ${timeStr}`).valueOf();
  return new Date(time);
};

ClassFormModel.getTimeStr = function (timeStr, formatValue) {
  if (SysUtil.isEmpty(timeStr)) {
    return '';
  }
  if (formatValue === undefined) {
    formatValue = 'HH:mm';
  }
  return moment(`${this.date} ${timeStr}`).format(formatValue);
};

ClassFormModel.date = moment().format('YYYY-MM-DD');

ClassFormModel.timeWayList = [{
  type: 1,
  name: '1天1次',
  timeList: [{
    startTime: ClassFormModel.getTimeDate('09:00'),
    endTime: ClassFormModel.getTimeDate('18:00'),
    startTimeRange: '00:00:00 - 18:00:00',
    endTimeRange: '09:00:00 - 23:59:00',
    earlyTime: null,
    lateTime: null,
    earlyTimeRange: '00:00:00 - 09:00:00',
    lateTimeRange: '18:00:00 - 23:59:00'
  }]
}, {
  type: 2,
  name: '1天2次',
  timeList: [{
    startTime: ClassFormModel.getTimeDate('09:00'),
    endTime: ClassFormModel.getTimeDate('12:00'),
    startTimeRange: '00:00:00 - 12:00:00',
    endTimeRange: '9:00:00 - 14:00:00',
    earlyTime: null,
    lateTime: null,
    earlyTimeRange: '00:00:00 - 09:00:00',
    lateTimeRange: '12:00:00 - 14:00:00'
  }, {
    startTime: ClassFormModel.getTimeDate('14:00'),
    endTime: ClassFormModel.getTimeDate('18:00'),
    startTimeRange: '12:00:00 - 18:00:00',
    endTimeRange: '14:00:00 - 23:59:00',
    earlyTime: null,
    lateTime: null,
    earlyTimeRange: '12:00:00 - 14:00:00',
    lateTimeRange: '18:00:00 - 23:59:00'
  }]
}, {
  type: 3,
  name: '1天3次',
  timeList: [{
    startTime: ClassFormModel.getTimeDate('09:00'),
    endTime: ClassFormModel.getTimeDate('11:00'),
    startTimeRange: '00:00:00 - 11:00:00',
    endTimeRange: '9:00:00 - 12:00:00',
    earlyTime: null,
    lateTime: null,
    earlyTimeRange: '00:00:00 - 09:00:00',
    lateTimeRange: '11:00:00 - 12:00:00'
  }, {
    startTime: ClassFormModel.getTimeDate('12:00'),
    endTime: ClassFormModel.getTimeDate('15:00'),
    startTimeRange: '11:00:00 - 15:00:00',
    endTimeRange: '12:00:00 - 16:00:00',
    earlyTime: null,
    lateTime: null,
    earlyTimeRange: '11:00:00 - 12:00:00',
    lateTimeRange: '15:00:00 - 16:00:00'
  }, {
    startTime: ClassFormModel.getTimeDate('16:00'),
    endTime: ClassFormModel.getTimeDate('18:00'),
    startTimeRange: '15:00:00 - 18:00:00',
    endTimeRange: '16:00:00 - 23:59:00',
    earlyTime: null,
    lateTime: null,
    earlyTimeRange: '15:00:00 - 16:00:00',
    lateTimeRange: '18:00:00 - 23:59:00'
  }]
}];
